package com.hospital.hospitalsystem.utils;

import com.hospital.hospitalsystem.config.HospitalProperties;
import com.hospital.hospitalsystem.modules.login.dto.EmailEntityExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author qiancheng@cqxyy.net
 * @date 2022/12/29
 */
@Component
public class SendEmailUtils {
    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private HospitalProperties giftProperties;

    @Async
    public void sendEmails(EmailEntityExt emailEntity){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(emailEntity.getEmail());
        message.setFrom(giftProperties.getSendEmail());
        if (StringUtils.isBlank(emailEntity.getSubject())) {
            message.setSubject("中医诊所在线预约系统");
        }else {
            message.setSubject(emailEntity.getSubject());
        }
        message.setText(emailEntity.getData());
        try{
            mailSender.send(message);
        }catch (Exception e){
            throw new RuntimeException("发送失败，请检查电子邮箱是否正确");
        }

    }
}
